/**
 * zmn文章模块-文章列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */

layui.define(['index', 'form', 'table', 'admin', 'layedit', 'laydate'], function (exports) {
    window.$ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let admin = layui.admin;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let parentId = $("#parentId").val();
    let tableRender = function (data) {
        data.parentId = parentId;
        zmnLayUtil.layTableRender(table, cols, "zmn-part-category-second-table", data, "/part/category/second/ajax/list.action");
    };
    let cols = [
        [
            {type: 'checkbox', fixed: 'left', width: 50, align: 'center'},
            {field: 'categId', title: 'ID', width: 80, align: 'center', unresize: true, sort: false},
            // {type: 'numbers', title: '序号', width: 40, align: 'center', unresize: true, sort: false},
            {field: 'name', title: '配件二级分类', align: 'center'},
            {field: 'parentName', title: '配件一级分类', width: 150, align: 'center', templet: '<div>{{parentPartCategoryName}}</div>'},
            {field: 'status', title: '状态', width: 80, align: 'center', unresize: true, templet: '<div>{{zmnEnum.STATUS[d.status]}}</div>'},
            {field: 'creater', title: '创建人', width: 80, align: 'center'},
            {field: 'createTime', title: '创建时间', width: 150, align: 'center', templet: '<div>{{zmnUtil.createTime(d.createTime)}}</div>'},
            {fixed: 'right', title: '操作', width: 80, align: 'center', toolbar: '#zmn-part-category-second-table-bar'}
        ]
    ];


    //监听表单提交
    form.on('submit(query-submit)', function (data) {
        zmnMothedUtil.deleteEmptyProperty(data.field);
        tableRender(data.field);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    //日期
    laydate.render({
        elem: '#updateStartEndDate'
        , range: true
    });

    //【表格右侧工具栏监听】
    table.on('tool(zmn-part-category-second-table)', function (obj) {
        console.log(obj);
        var categId = obj.data.categId;
        if (obj.event === 'enable') {
            $.post("/part/category/second/ajax/enable.action", {categId: categId}).done(function () {
                table.reload("zmn-part-category-second-table");
            });
        } else if (obj.event === 'disable') {
            layer.confirm('真的要禁用吗？', function (index) {
                $.post("/part/category/second/ajax/disable.action", {categId: categId}).done(function () {
                    table.reload("zmn-part-category-second-table");
                });
                layer.close(index);
            });

        } else if (obj.event === 'detail') {
            zmnLayUtil.openIframeCustom(layer, "/part/category/second/list/addEdit.action?parentId=" + parentId + "&categId=" + categId, {
                title: "编辑配件二级分类", area: ["400px", "350px"]
            })

        }
    });

    //【表格上方工具栏监听】
    table.on("toolbar(zmn-part-category-second-table)", function (obj) {

        if (obj.event === "add") {
            zmnLayUtil.openIframeCustom(layer, "/part/category/second/list/addEdit.action?parentId=" + parentId, {
                title: "新增配件二级分类", area: ["400px", "350px"]
            });

        } else if (obj.event === "cancel") {
            admin.events.closeThisTabs();
            zmnLayUtil.closeThisTabs()
        } else if (obj.event === "start" || obj.event === "stop") {
           let  ids = table.checkStatus('zmn-part-category-second-table').data.map((item) => item.categId);
            if (ids.length < 1) {
                layer.msg('请选择至少一个要操作的配件分类', {icon: 2});
                return false;
            }

            zmnLayUtil.tableEnableDisable($, obj, "/part/category/second/ajax/updateStatus.action", ids, function (data) {
                table.reload("zmn-part-category-second-table");
            });
        }

        return false;

    });

    //单元格编辑
    /* table.on('edit(zmn-part-category-second-table)', function (obj) {

         $.post("/part/category/second/editname.action", {categId: obj.data.categId, name: obj.data.name}).done(
             function (obj) {
                 console.log(obj);
                 table.reload("zmn-part-category-second-table");
             });
     });*/


    $("#query-submit").click();
});